package com.nanako.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nanako.common.R;
import com.nanako.entity.Employee;
import com.nanako.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    public R login(HttpServletRequest request, @RequestBody Employee employee) {

        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        HttpSession session = request.getSession();
        session.setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 员工退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 添加员工
     *
     * @param employee 要添加的员工数据
     * @return
     */
    @PostMapping
    public R saveEmployee(HttpServletRequest request, @RequestBody Employee employee) {

        HttpSession session = request.getSession();
        System.out.println("saveEmployee = " + session);
        // 对员工的密码进行加密
        employee.setPassword(
                DigestUtils.md5DigestAsHex("123456".getBytes())
        );
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 员工列表分页查询
     * @param page 当前页码
     * @param pageSize 当前页有多少条数据
     * @param name 查询的员工姓名
     */
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        IPage<Employee> curPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        employeeService.page(curPage, wrapper);
        return R.success(curPage);
    }

    /**
     * 修改员工
     * @param request 获取session
     * @param employee 要修改的对象
     */
    @PutMapping
    public R updateEmployee(HttpServletRequest request, @RequestBody Employee employee) {
        if (employeeService.updateById(employee)) {
            return R.success("修改成功☆*: .｡. o(≧▽≦)o .｡.:*☆");
        }
        return R.error("修改失败≡(▔﹏▔)≡");
    }

    @GetMapping("/{id}")
    public R getOne(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if (ObjectUtils.isNotEmpty(employee)) {
            return R.success(employee);
        }
        return R.error("该数据不存在");
    }

}
